/*
 * File:   UtilsTest.cpp
 * Author: Luis
 *
 * Created on 03-abr-2013, 15:02:27
 */

#include "UtilsTest.h"
#include "../../src/utils/Utils.h"

using namespace zipUtils;

CPPUNIT_TEST_SUITE_REGISTRATION(UtilsTest);

UtilsTest::UtilsTest() {
}

UtilsTest::~UtilsTest() {
}

void UtilsTest::setUp() {
}

void UtilsTest::tearDown() {
}

void UtilsTest::testCalculateSize() {
    FILE* file = fopen("utils/test.txt", "r");
    long result = calculateSize(file);
    long expected = 38;

    CPPUNIT_ASSERT(result == expected);
}

void UtilsTest::testGetBytes() {
    FILE* file = fopen("utils/test.txt", "r");
    long size = calculateSize(file);
    char* buffer = (char*) malloc(size + 1);
    getBytes(file, buffer, size);
    int result = strlen(buffer);
    CPPUNIT_ASSERT(size == result);
}

void UtilsTest::testGetCRC32() {
    FILE* file = fopen("utils/test.txt", "r");
    long result = getCRC32(file);
    long expected = 0xE98E465A;
    CPPUNIT_ASSERT(result == expected);
}

void UtilsTest::testGetLastModificationTimeAndDate()
{
    char buffer[22];
    char expected[] = "2013/04/03 20:04:46";
    tm* date = getLastModificationTimeAndDate("Utils/test.txt");
    strftime(buffer, 22, "%Y/%m/%d %H:%M:%S", date);
    CPPUNIT_ASSERT(strcmp(buffer, expected) == 0);
}

void UtilsTest::testParseDateToShort()
{
    unsigned short expected = 17027;
    tm* tmLastModified = getLastModificationTimeAndDate("Utils/test.txt");
    unsigned short date = parseDateToShort(tmLastModified);
    
    CPPUNIT_ASSERT(expected == date);
}

void UtilsTest::testParseTimeToShort()
{
    unsigned short expected = 41111;
    tm* tmLastModified = getLastModificationTimeAndDate("Utils/test.txt");
    unsigned short time = parseTimeToShort(tmLastModified);
    
    CPPUNIT_ASSERT(expected == time);
}

void UtilsTest::testIsCorruptFileFunctionWhitANonCorruptFile(){
    bool expected = false;
    bool actual = isCorruptFile("Utils/Folder.zip");
    
    CPPUNIT_ASSERT_ASSERTION_PASS(CPPUNIT_ASSERT(expected == actual));
}